﻿using QLKetNoi;
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLDVu.BO
{
    class PhieuSuaChuaBO
    {
        QLDNDataContext Database = new QLDNDataContext();

        int SoMaCanTao = 0;

        public BAOGIA LayTongTien(string mabaogia)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<BAOGIA> phieusuachua = dulieu.GetTable<BAOGIA>();
            BAOGIA x = phieusuachua.Single(xx => xx.MaBaoGia.Equals(mabaogia));
            return x;
        }
        public SUACHUA LayThongTinSuaChua(string MaBaoGia)
        {
            return Database.SUACHUAs.Where(p => p.MaBaoGia.Equals(MaBaoGia.Trim())).FirstOrDefault();
        }


        public IQueryable LayDanhSachPhieuSua()
        {
            return from p in Database.SUACHUAs
                   select new
                   {
                       MaPhieuSua = p.MaSuaChua,
                       BienSo = p.BAOGIA.BienSoXe,
                       TenKhachHang = p.BAOGIA.KHACHHANG.TenKhachHang,
                       NgayLap = p.NgayVao
                   };
        }


        public IQueryable LayDanhSachPhieuSua(string BienSoXe)
        {
            return from p in Database.SUACHUAs
                   where p.BAOGIA.BienSoXe.Equals(BienSoXe.Trim())
                   select new
                   {
                       MaPhieuSua = p.MaSuaChua,
                       BienSo = p.BAOGIA.BienSoXe,
                       TenKhachHang = p.BAOGIA.KHACHHANG.TenKhachHang,
                       NgayLap = p.NgayVao
                   };
        }


        public void LayThongTinBaoGiaSuaChua(string MaBaoGia, out Dictionary<string, string> DicBaoGia, out List<string> ListDichVu)
        {
            DicBaoGia = new Dictionary<string, string>();
            ListDichVu = new List<string>();

            var ThongTin = from bg in Database.BAOGIAs
                           join kh in Database.KHACHHANGs on bg.MaKhachHang equals kh.MaKhachHang
                           join ctbg in Database.CHITIETBAOGIAs on bg.MaBaoGia equals ctbg.MaBaoGia
                           join dv in Database.DICHVUs on ctbg.MaDichVu equals dv.MaDichVu into lrj
                           from lr in lrj.DefaultIfEmpty()
                           where bg.MaBaoGia == MaBaoGia
                           select new
                           {
                               SoBaoGia = bg.MaBaoGia,
                               NgayLap = bg.NgayLap,
                               BienSo = bg.BienSoXe,
                               TenKhach = kh.TenKhachHang,
                               DienThoai = kh.DienThoai,
                               Email = kh.Email,
                               DiaChi = kh.DiaChi,
                               TenDichVu = lr.TenDichVu
                           };

            foreach (var item in ThongTin)
            {
                DicBaoGia.Add("SoBaoGia", item.SoBaoGia);
                DicBaoGia.Add("NgayLap", item.NgayLap.ToShortDateString());
                DicBaoGia.Add("BienSo", item.BienSo);
                DicBaoGia.Add("TenKhach", item.TenKhach);
                DicBaoGia.Add("DienThoai", item.DienThoai);
                DicBaoGia.Add("Email", item.Email);
                DicBaoGia.Add("DiaChi", item.DiaChi);
                break;
            }

            foreach (var item in ThongTin)
            {
                if (item.TenDichVu != null)
                {
                    ListDichVu.Add(item.TenDichVu);
                    SoMaCanTao++;
                }
            }

        }


        public List<string> LayDanhSachNhanVien()
        {
            List<string> result = new List<string>();
            var TenND = from nv in Database.NGUOIDUNGs
                        select new { TenNhanVien = nv.TenNguoiDung };
            foreach (var s in TenND)
            {
                result.Add(s.TenNhanVien);
            }
            return result;
        }

        public SUACHUA LayThongTinPhieuSuaTheoMaBaoGia(string MaBaoGia)
        {
            return Database.SUACHUAs.Where(p => p.MaBaoGia == MaBaoGia.Trim()).FirstOrDefault();
        }


        public SUACHUA LayThongTinPhieuSuaTheoMaPhieuSua(string MaPhieuSua)
        {
            return Database.SUACHUAs.Where(p => p.MaSuaChua == MaPhieuSua.Trim()).FirstOrDefault();
        }


        public void ThemPhieuSuaChua(SUACHUA SuaChua)
        {
            Database.SUACHUAs.InsertOnSubmit(SuaChua);
            Database.SubmitChanges();
        }

        public void XoaPhieuSuaChua(string MaPhieuSua)
        {
            var PhieuSua = Database.SUACHUAs.Where(p => p.MaSuaChua == MaPhieuSua.Trim()).FirstOrDefault();
            Database.SUACHUAs.DeleteOnSubmit(PhieuSua);
            Database.SubmitChanges();
        }















        public void SuaThongTinPhieuSua(string MaPhieuSua, DateTime NgayVao, string TinhTrang, int SoKmVao, DateTime NgayRa, int SoKmRa, string GhiChu)
        {
            var PhieuSua = Database.SUACHUAs.Where(p => p.MaSuaChua == MaPhieuSua.Trim()).FirstOrDefault();
            PhieuSua.NgayVao = NgayVao;
            PhieuSua.NgayHoanThanh = NgayRa;
            PhieuSua.SoKmVao = SoKmVao;
            PhieuSua.SoKmRa = SoKmRa;
            PhieuSua.GhiChu = GhiChu;
            PhieuSua.TinhTrang = TinhTrang;
            Database.SubmitChanges();
        }

        public void SuaThongTinPhieuSua(string MaPhieuSua, DateTime NgayVao, string TinhTrang, DateTime NgayRa, int SoKmRa, string GhiChu)
        {
            var PhieuSua = Database.SUACHUAs.Where(p => p.MaSuaChua == MaPhieuSua.Trim()).FirstOrDefault();
            PhieuSua.NgayVao = NgayVao;
            PhieuSua.NgayHoanThanh = NgayRa;
            PhieuSua.SoKmRa = SoKmRa;
            PhieuSua.SoKmVao = null;
            PhieuSua.GhiChu = GhiChu;
            PhieuSua.TinhTrang = TinhTrang;
            Database.SubmitChanges();
        }

        public void SuaThongTinPhieuSua(string MaPhieuSua, DateTime NgayVao, string TinhTrang, int SoKmVao, DateTime NgayRa, string GhiChu)
        {
            var PhieuSua = Database.SUACHUAs.Where(p => p.MaSuaChua == MaPhieuSua.Trim()).FirstOrDefault();
            PhieuSua.NgayVao = NgayVao;
            PhieuSua.NgayHoanThanh = NgayRa;
            PhieuSua.SoKmVao = SoKmVao;
            PhieuSua.SoKmRa = null;
            PhieuSua.GhiChu = GhiChu;
            PhieuSua.TinhTrang = TinhTrang;
            Database.SubmitChanges();
        }

        public void SuaThongTinPhieuSua(string MaPhieuSua, DateTime NgayVao, string TinhTrang, DateTime NgayRa, string GhiChu)
        {
            var PhieuSua = Database.SUACHUAs.Where(p => p.MaSuaChua == MaPhieuSua.Trim()).FirstOrDefault();
            PhieuSua.NgayVao = NgayVao;
            PhieuSua.NgayHoanThanh = NgayRa;
            PhieuSua.SoKmVao = null;
            PhieuSua.SoKmRa = null;
            PhieuSua.GhiChu = GhiChu;
            PhieuSua.TinhTrang = TinhTrang;
            Database.SubmitChanges();
        }














        public string SinhMaSuaChua()
        {
            string s = "";
            try
            {
                s = Database.SUACHUAs.OrderByDescending(h => h.MaSuaChua).FirstOrDefault().MaSuaChua;
            }
            catch
            {

            }
            string result = "";
            if (s != "")
                result = NextID(s, "SC");
            else
                result = "SC0001";
            return result;
        }





        public List<string> SinhMaChiTietSuaChua(int SoMaCanTao)
        {
            List<string> result = new List<string>();
            string s = "";
            try
            {
                s = Database.CHITIETPHIEUSUACHUAs.OrderByDescending(h => h.MaChiTietSuaChua).FirstOrDefault().MaChiTietSuaChua;
            }
            catch
            {

            }
            if (s != "")
                result = NextIDs(s, "CTSC", SoMaCanTao);
            else
            {
                for (int i = 1; i <= SoMaCanTao; i++)
                    result.Add("CTSC000" + i.ToString());
            }
            return result;
        }



        public static List<string> NextIDs(string lastID, string prefixID, int SoMaCanTao)
        {
            List<string> result = new List<string>();

            for (int k = 1; k <= SoMaCanTao; k++)
            {
                int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + k;
                int lengthNumerID = lastID.Length - prefixID.Length;
                string zeroNumber = "";
                for (int i = 1; i <= lengthNumerID; i++)
                {
                    if (nextID < Math.Pow(10, i))
                    {
                        for (int j = 1; j <= lengthNumerID - i; i++)
                        {
                            zeroNumber += "0";
                        }
                        result.Add(prefixID + zeroNumber + nextID.ToString());
                    }
                }
                //result.Add(prefixID + nextID);
            }

            return result;
        }


        public static string NextID(string lastID, string prefixID)
        {
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;
        }



        public void LayThongTinXe(string BienSo, ref Dictionary<string, string> TTPhieuSua)
        {
            var kq = from Xe in Database.XEs
                     join DoiXe in Database.DOIXEs on Xe.MaDoiXe equals DoiXe.MaDoiXe into lrj
                     from lr in lrj.DefaultIfEmpty()
                     join DongXe in Database.DONGXEs on Xe.MaDongXe equals DongXe.MaDongXe into lrj1
                     from lr1 in lrj1.DefaultIfEmpty()
                     join HangSX in Database.HANGSXes on Xe.MaHangSanXuat equals HangSX.MaHangSanXuat into lrj2
                     from lr2 in lrj2.DefaultIfEmpty()
                     where Xe.BienSoXe.Equals(BienSo.Trim())
                     select new
                     {
                         BienSo = Xe.BienSoXe,
                         SoSuon = Xe.SoSuon,
                         HangSanXuat = lr2.TenHangSanXuat,
                         DongXe = lr1.TenDongXe,
                         DoiXe = lr.TenDoiXe
                     };

            foreach (var item in kq)
            {
                TTPhieuSua.Add("BienSo", item.BienSo);
                TTPhieuSua.Add("SoSuon", item.SoSuon);
                TTPhieuSua.Add("HangSanXuat", item.HangSanXuat);
                TTPhieuSua.Add("DongXe", item.DongXe);
                TTPhieuSua.Add("DoiXe", item.DoiXe);
                break;
            }
        }



    }
}
